package com.ram.app.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ram.app.mapper.RamUserMapper;
import com.ram.app.service.RamUserService;
import com.ram.common.entity.RamUser;
import com.ram.common.entity.wrapper.UserWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fudongjing
 * @since 2018-07-08
 */
@Service
public class RamUserServiceImpl extends ServiceImpl<RamUserMapper, RamUser> implements RamUserService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserWrapper getUserByName(String username) throws Exception {
        return this.baseMapper.getUserByName(username);
    }

    @Override
    public Boolean registerUser(RamUser user) throws Exception {
       //对密码进行加密
       String password = passwordEncoder.encode(user.getPassword());
       user.setPassword(password);
       user.setLastPasswordUpdate(new Date());
       user.setCreateDate(new Date());
       user.setEnable(Boolean.TRUE);
       return this.insert(user);
    }

    @Override
    public Boolean valitedPhone(String phoneNum) throws Exception {
        Wrapper<RamUser> wrapper = new EntityWrapper<>();
        wrapper.eq("phone",phoneNum);
        return this.selectCount(wrapper)>0;
    }
}
